package TeleRobot;

import static TeleRobot.MyPanel.TargetPoint;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;

class MyPanel extends JPanel
{
    private static int grid_spacing = 25;
    public static final Color background_color = Color.WHITE;
    public static final Color selection_color = Color.RED;
    private static final Dimension preferred_size = new Dimension(700,700);
    private ArrayList<Point> gridPoints = new ArrayList<Point>();
    public static Point selectedPoint = null;
    public static JLabel TargetPoint = new JLabel("");
    public static JLabel WorldStatus = new JLabel("");
    
    public MyPanel()
    {
        setBorder(BorderFactory.createLineBorder(Color.black));
        setBackground(Color.WHITE);
    }
    
    public MyPanel(Graphics g)
    {
        //Call other constructor
        this(g, grid_spacing);
    }
    
    public MyPanel(Graphics g, int spacing)
    {
        grid_spacing = spacing;
        setPreferredSize(preferred_size);
        setSize(preferred_size);
        setBackground(background_color);
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        setLayout(new BorderLayout());
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(700,700);
    }
    
    public void drawGrid(Graphics g)
    {
        g.setColor(Color.BLACK);
        for (int i = 0; i<getWidth(); i+=grid_spacing)
            for (int j=0; j<getHeight(); j+=grid_spacing)
            {
                g.drawOval(i, j, 2, 2);
                gridPoints.add(new Point(i, j));
            }   
    }
    
    public void drawSelectedPoint(Graphics g)
    {
       if (selectedPoint != null)
       {
           g.setColor(selection_color);
           int grid_x = (int)selectedPoint.x;
           int grid_y = (int)selectedPoint.y;
           int line_length = 5;
           int oval_size = 6;
           g.drawOval(grid_x, grid_y, oval_size, oval_size);
           g.drawLine(grid_x+oval_size, grid_y + (oval_size/2), grid_x+oval_size+line_length, grid_y + (oval_size/2));
           g.drawLine(grid_x+(oval_size/2), grid_y, grid_x+(oval_size/2), grid_y-line_length);
           g.drawLine(grid_x-line_length, grid_y + (oval_size/2), grid_x, grid_y + (oval_size/2));
           g.drawLine(grid_x+(oval_size/2), grid_y+oval_size, grid_x+(oval_size/2), grid_y+oval_size+line_length);
       }
       
    }
    
    public Point closestGridPoint(Point input_coordinate)
    {
        Point closestPoint = input_coordinate;
        double min_distance = Double.MAX_VALUE;
        
        for (Point tempPoint: gridPoints)
        {
            double curr_distance = Point.distance(tempPoint.x, tempPoint.y, input_coordinate.x, input_coordinate.y);
            if (curr_distance < min_distance)
            {
                min_distance = curr_distance;
                closestPoint = tempPoint;
            }
            
            if (curr_distance == 0)
                break;
        }
        return closestPoint;
    }
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);       
        RoboticsApp.robo.draw(g);
        drawGrid(g);
        drawSelectedPoint(g);
    }
}


public class MyFrame extends JFrame 
{
    double currentT1;
    double currentT2;
    double currentT3;
    public JLabel label_thetaone= new JLabel("THETA 1:");
    public JLabel label_thetatwo= new JLabel("THETA 2:");
    public JLabel label_thetathree= new JLabel("THETA 3:");
    
    public MyFrame()
    {
        
        super();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        if(!RoboticsApp.server_mode)
        {
            //Add Control Buttons
            Dimension ButtonSize = new Dimension(20, 20); 
            JButton plusThetaOne = new JButton("CW");
            plusThetaOne.setSize(ButtonSize);

            JButton plusThetaTwo= new JButton("CW");
            plusThetaTwo.setSize(ButtonSize);

            JButton plusThetaThree = new JButton("CW");
            plusThetaThree.setSize(ButtonSize);

            JButton minusThetaOne = new JButton("CCW");
            minusThetaOne.setSize(ButtonSize);

            JButton minusThetaTwo = new JButton("CCW");
            minusThetaTwo.setSize(ButtonSize);

            JButton minusThetaThree = new JButton("CCW");
            minusThetaOne.setSize(ButtonSize);

            JToggleButton paintButton = new JToggleButton("ON/OFF"); //PAINT TOGGLE
            paintButton.setSize(40,40);

            // Add arrow buttons for paint brush

            JButton arrowRight = new BasicArrowButton(BasicArrowButton.EAST);
            arrowRight.setSize(30,30);
            JButton arrowLeft = new BasicArrowButton(BasicArrowButton.WEST);
            arrowLeft.setSize(30,30);
            JButton arrowUp = new BasicArrowButton(BasicArrowButton.NORTH);
            arrowUp.setSize(30,30);
            JButton arrowDown = new BasicArrowButton(BasicArrowButton.SOUTH);
            arrowDown.setSize(30,30);
        
            //Add labels for each button

            JLabel label_jointmode = new JLabel("JOINT MODE");
            label_jointmode.setFont(new Font("Serif", Font.BOLD, 10));
            JLabel painttoggle = new JLabel("PAINT: ");
            JLabel label_coordinate = new JLabel("Target Coordinate:");
            JLabel label_worldstatus = new JLabel("Status:");
            JLabel label_brushUpDown = new JLabel("Brush +Y/-Y: ");
            JLabel label_brushLeftRight = new JLabel("Brush -X/+X: ");

            //Create container for "Joint Mode" Control Buttons
            JPanel buttonsPanel = new JPanel();

            int num_buttonrows = 6;

           // int num_buttonrows = 9;

            int num_buttoncols = 3;
            int button_spacing = 10;
            buttonsPanel.setLayout(new GridLayout(num_buttonrows,num_buttoncols,button_spacing,button_spacing)); 
            Dimension buttonsPanel_size = new Dimension(280,250);
            buttonsPanel.setSize(buttonsPanel_size);
            buttonsPanel.setPreferredSize(buttonsPanel_size);

            //Row 1
            buttonsPanel.add(new JLabel(""));
            buttonsPanel.add(label_jointmode);
            buttonsPanel.add(new JLabel(""));
            //Row 2
            buttonsPanel.add(label_thetaone);
            buttonsPanel.add(minusThetaOne);
            buttonsPanel.add(plusThetaOne);
            //Row 3
            buttonsPanel.add(label_thetatwo);
            buttonsPanel.add(minusThetaTwo);
            buttonsPanel.add(plusThetaTwo);
            //Row 4
            buttonsPanel.add(label_thetathree);
            buttonsPanel.add(minusThetaThree);
            buttonsPanel.add(plusThetaThree);

            buttonsPanel.add(painttoggle);
            buttonsPanel.add(paintButton);
            buttonsPanel.add(new JLabel(""));
            buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

            //Create "Joint Mode Control" panel
            JPanel controlPanel = new JPanel();
            controlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            Dimension controlPanel_size = new Dimension(350,350);
            controlPanel.setSize(controlPanel_size);
            controlPanel.setPreferredSize(controlPanel_size);
            controlPanel.add(buttonsPanel);

            //Create "brush Control" panel
            JPanel brushControlPanel = new JPanel();
            int num_brushrows = 3;
            int num_brushcols = 3;
            brushControlPanel.setLayout(new GridLayout(num_brushrows,num_brushcols,button_spacing,button_spacing)); 

            //Row 1
            JLabel label_worldmode = new JLabel("WORLD MODE");
            label_worldmode.setFont(new Font("Serif", Font.BOLD, 10));
            brushControlPanel.add(new JLabel(""));
            brushControlPanel.add(label_worldmode);
            brushControlPanel.add(new JLabel(""));

            //Row 2
            brushControlPanel.add(label_brushUpDown);
            brushControlPanel.add(arrowUp);
            brushControlPanel.add(arrowDown);

            //Row 3
            brushControlPanel.add(label_brushLeftRight);
            brushControlPanel.add(arrowLeft);
            brushControlPanel.add(arrowRight);
            brushControlPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));


            //Create "World Mode Control" panel
            JPanel worldControlPanel = new JPanel();
            int num_worldrows = 4;
            int num_worldcols = 2;
            worldControlPanel.setLayout(new GridLayout(num_worldrows,num_worldcols,button_spacing,button_spacing));
            worldControlPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

            //Row1
            JLabel label_info = new JLabel("SYSTEM INFO:");
            label_info.setFont(new Font("Serif", Font.BOLD, 10));
            worldControlPanel.add(new JLabel(""));
            worldControlPanel.add(label_info);


            //Row2
            worldControlPanel.add(label_coordinate);
            worldControlPanel.add(MyPanel.TargetPoint);

            //Row3
            worldControlPanel.add(label_worldstatus);
            worldControlPanel.add(MyPanel.WorldStatus);

            //Row4
            //worldControlPanel.add(brushControlPanel);

            controlPanel.add(brushControlPanel);
            controlPanel.add(worldControlPanel);
            final MyPanel drawingArea = new MyPanel(getGraphics());
            add(controlPanel, BorderLayout.EAST);
            add(drawingArea, BorderLayout.WEST);

        plusThetaOne.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                  System.out.println("Joint 1 CW Button was Clicked");
                 RoboticsApp.robo.redraw(getGraphics(),1, -1);
                 repaint();
              }
          });
        plusThetaTwo.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                  System.out.println("Joint 2 CW Button was Clicked");
                 RoboticsApp.robo.redraw(getGraphics(),2, -1);
                 repaint();
              }
          });
        plusThetaThree.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                  System.out.println("Joint 3 CW Button was Clicked");
                 RoboticsApp.robo.redraw(getGraphics(),3, -1);
                 repaint();
              }
          });
        minusThetaOne.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                  System.out.println("Joint 1 CCW Button was Clicked"); 
                 RoboticsApp.robo.redraw(getGraphics(),1, 1);
                 repaint();
              }
          });
        minusThetaTwo.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                  System.out.println("Joint 2 CCW Button was Clicked"); 
                 RoboticsApp.robo.redraw(getGraphics(),2, 2);
                 repaint();
              }
          });
        minusThetaThree.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                  System.out.println("Joint 3 CCW Button was Clicked");
                 RoboticsApp.robo.redraw(getGraphics(),3, 5);
                 repaint();
              }
          });
        paintButton.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                 System.out.println("Paint Button was Clicked");             
                 RoboticsApp.robo.togglePaint();
                 //RoboticsApp.robo.paint(getGraphics());
              }
          });
        arrowLeft.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println("Left Arrow Button was Clicked");
                  //----Sample code to move paintbrush --use if needed
                  Point target = RoboticsApp.robo.getBrushLocation().clone();
                  target.x = target.x - 5;
                  MyPanel.selectedPoint = target;
                  TargetPoint.setText(String.valueOf(MyPanel.selectedPoint));
                  if(RoboticsApp.robo.animateJoints(getGraphics(), target)) {
                      repaint();
                      MyPanel.WorldStatus.setText("Success!");
                  }
                  else
                      MyPanel.WorldStatus.setText("Invalid Point!");
              }
        });
        arrowRight.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println("Right Arrow Button was Clicked");
                  //RoboticsApp.robo.moveBrush("x", 5);
                  Point target = RoboticsApp.robo.getBrushLocation().clone();
                  target.x = target.x + 5;
                  MyPanel.selectedPoint = target;
                  TargetPoint.setText(String.valueOf(MyPanel.selectedPoint));
                  if(RoboticsApp.robo.animateJoints(getGraphics(), target)) {
                      repaint();
                      MyPanel.WorldStatus.setText("Success!");
                  }
                  else
                      MyPanel.WorldStatus.setText("Invalid Point!");
              }
        });
        arrowUp.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println("Arrow Up Button was Clicked");
                  //RoboticsApp.robo.moveBrush("y", 5);
                  Point target = RoboticsApp.robo.getBrushLocation().clone();
                  target.y = target.y - 5;
                  MyPanel.selectedPoint = target;
                  TargetPoint.setText(String.valueOf(MyPanel.selectedPoint));
                  if(RoboticsApp.robo.animateJoints(getGraphics(), target)) {
                      repaint();
                      MyPanel.WorldStatus.setText("Success!");
                  }
                  else
                      MyPanel.WorldStatus.setText("Invalid Point!");
              }
        });
        arrowDown.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println("Arrow Down Button was Clicked");
                  //RoboticsApp.robo.moveBrush("y", -5);
                  Point target = RoboticsApp.robo.getBrushLocation().clone();
                  target.y = target.y + 5;
                  MyPanel.selectedPoint = target;
                  TargetPoint.setText(String.valueOf(MyPanel.selectedPoint));
                  if(RoboticsApp.robo.animateJoints(getGraphics(), target)) {
                      repaint();
                      MyPanel.WorldStatus.setText("Success!");
                  }
                  else
                      MyPanel.WorldStatus.setText("Invalid Point!");
              }
        });

        drawingArea.addMouseListener(new MouseAdapter()
        {
              @Override
              public void mousePressed(MouseEvent e)
              {
                  //Display click location
                  System.out.println("Mouse Location:"+ String.valueOf(e.getPoint()));
                  Graphics canvas = getGraphics();

                  //Select current point
                  Point mouse_location = new Point(e.getPoint());
                  MyPanel.selectedPoint = mouse_location.clone();
                  TargetPoint.setText(String.valueOf(MyPanel.selectedPoint));

                  if(RoboticsApp.robo.animateJoints(canvas, MyPanel.selectedPoint,RoboticsApp.mainFrame))
                  {
                      MyPanel.WorldStatus.setText("Success!");
                      //repaint();
                      Point target = MyPanel.selectedPoint;
                      Point curr = RoboticsApp.robo.getBrushLocation();
                      double err = Point.distance(target.x, target.y,curr.x, curr.y);
                      System.out.println("Err:"+String.valueOf(err));
                  }
                  else
                      MyPanel.WorldStatus.setText("Invalid Point!");

             }
         });
        }
       
    }
    

}
